package com.xtck.ha.fault.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xtck.ha.fault.framework.page.PageResult;
import com.xtck.ha.fault.framework.query.Query;
import com.xtck.ha.fault.modules.convert.FmeaInfoConvert;
import com.xtck.ha.fault.modules.dao.FmeaInfoDAO;
import com.xtck.ha.fault.modules.pojo.dto.FmeaInfoDTO;
import com.xtck.ha.fault.modules.pojo.entity.FmeaInfo;
import com.xtck.ha.fault.modules.pojo.vo.FmeaInfoVO;
import com.xtck.ha.fault.modules.service.FmeaInfoService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
* @Description: FmeaInfoService实现类
* @author liuyuan
* @date 2025-06-17
*/
@Service
@AllArgsConstructor
public class FmeaInfoServiceImpl extends ServiceImpl<FmeaInfoDAO, FmeaInfo>  implements FmeaInfoService {
        @Autowired
        private FmeaInfoDAO fmeaInfoDAO;
        /**
        * 获取分页对象
        * @param query   分页参数
        */
        protected IPage<FmeaInfo> getPage(Query query) {
                Page<FmeaInfo> page = new Page<FmeaInfo>(query.getPage(), query.getLimit());
                // 排序
                if(StringUtils.isNotBlank(query.getOrder())){
                        if(query.isAsc()) {
                                return page.addOrder(OrderItem.asc(query.getOrder()));
                        }else {
                                return page.addOrder(OrderItem.desc(query.getOrder()));
                        }
                }
                return page;
        }
        @Override
        public PageResult<FmeaInfoVO> page(FmeaInfoDTO query) {
                IPage<FmeaInfo> page = fmeaInfoDAO.selectPage(getPage(query), getWrapper(query));
                return new PageResult<FmeaInfoVO>(FmeaInfoConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
        }

        private QueryWrapper<FmeaInfo> getWrapper(FmeaInfoDTO query){
                QueryWrapper<FmeaInfo> wrapper = new QueryWrapper<>();

                return wrapper;
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public FmeaInfo save(FmeaInfoVO vo) {
                FmeaInfo entity = FmeaInfoConvert.INSTANCE.convert(vo);
                baseMapper.insert(entity);
                return entity;
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public FmeaInfo update(FmeaInfoVO vo) {
                FmeaInfo entity = FmeaInfoConvert.INSTANCE.convert(vo);
                updateById(entity);
                return entity;
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public FmeaInfo getByTaskId(Long taskId) {
                return fmeaInfoDAO.selectByTaskId(taskId);
        }



        @Override
        @Transactional(rollbackFor = Exception.class)
        public void delete(List<Long> idList) {
                removeByIds(idList);
        }

}
